#!/bin/sh
#
#
# PROVIDE: django
# REQUIRE: DAEMON
# KEYWORD: shutdown
#

#-
# Copyright (c) 2010, 2011, 2015 iXsystems, Inc., All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL Jordan Hubbard OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

. /etc/rc.freenas

name="django"
rcvar="django_enable"

load_rc_config $name

: ${django_enable="YES"}
: ${django_pidfile="/var/run/${name}.pid"}
: ${django_sockfile="/tmp/${name}.sock"}
: ${django_path="/usr/local/www/freenasUI"}
: ${django_config="/etc/local_settings.py"}
: ${django_host="127.0.0.1"}
: ${django_port="9042"}

pidfile=${django_pidfile}
sockfile=${django_sockfile}
path=${django_path}
host=${django_host}
port=${django_port}
start_cmd="start_command"
sig_stop="INT"
procname="/usr/local/bin/uwsgi"

start_command()
{
	local logs="/var/log/nginx-error.log /var/log/nginx-access.log"
	new_string=`dd if=/dev/random bs=32 count=1 2>/dev/null | /usr/local/bin/base64`
	sed -i "" -e /SECRET_KEY.*/d ${django_config} 2> /dev/null
        # changed ">>" below to ">" as I want it to write a new file each time
        echo "SECRET_KEY = '${new_string}'" > ${django_config}
        local webguiproto=$(${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} "SELECT stg_guiprotocol from system_settings")
        if [ "${webguiproto}" = "https" -a ! -f /tmp/alert_invalid_ssl_nginx ]; then
            echo "SESSION_COOKIE_SECURE = True" >> ${django_config}
            echo "CSRF_COOKIE_SECURE = True" >> ${django_config}
        fi

	local proxy=$(${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} "SELECT gc_httpproxy FROM network_globalconfiguration ORDER by -id LIMIT 1")

	if [ -n "${proxy}" ]; then
		export http_proxy="${proxy}"
		export https_proxy="${proxy}"
	fi

	env LC_ALL=en_US.UTF-8 /usr/local/bin/uwsgi --chdir=/usr/local/www --module=freenasUI.wsgi:application --master --pidfile=${pidfile} --fastcgi-socket=${host}:${port} -T --threads=15 -L -b 20480 --daemonize=/var/log/uwsgi.log
	touch ${logs}
	/usr/local/bin/python ${path}/tools/alert.py > /dev/null 2>&1
	sleep 1
	chown www:www ${logs}
}

run_rc_command "$1"
